package clustering;

import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.jdt.core.dom.ASTRequestor;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.internal.corext.dom.Bindings;
import org.eclipse.jdt.internal.ui.text.correction.ASTResolving;

import com.aliasi.util.Distance;

public class MethodDistanceFinder
	implements Distance<Entry<MethodDeclaration, Set<ITypeBinding>>> {

	@Override
	public double distance(Entry<MethodDeclaration, Set<ITypeBinding>> arg0,
			Entry<MethodDeclaration, Set<ITypeBinding>> arg1) {
		Set<ITypeBinding> intersects = new HashSet<ITypeBinding>();
		for (ITypeBinding iTB0 : arg0.getValue())
			for (ITypeBinding iTB1 : arg1.getValue())
				if (iTB0.isEqualTo(iTB1))
					intersects.add(iTB1);
		
		/*if class' methods are related there is no way to split class
		* Checking all method pairs between clusters.*/
		MethodRelationshipDeterminer relationChecker = new MethodRelationshipDeterminer();
		
		if (relationChecker.areRelated(arg0.getKey(), arg1.getKey()))
			return 0;
		//System.out.println("Methods "+arg0.getKey().getName()+" and "+arg1.getKey().getName()+" use "+intersects.size()+" common classes");
		if (intersects.isEmpty())
			return 1000.0;
		return 1/intersects.size();
	}

}
